// components/luyin/luyin.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    luyinpop:false,
    luyinzhong:false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    luyin:function(){
      this.setData({
        luyinpop: true
      })

      wx.authorize({
        scope: 'scope.record',
        success() {
          console.log("录音授权成功");
        },
      })

      
    },
    noluyin: function () {
      this.setData({
        luyinpop: false,
        luyinzhong:false,
      })
    },
    luyintap:function(){

    },
    _handleRecordStart:function(e){
      var val = {
        e: e
      }
      var that = this;
      wx.authorize({
        scope: 'scope.record',
        success() {
          console.log("录音授权成功");
          that.setData({
            luyinzhong: true
          })
          that.triggerEvent("recordStart", val)
        },
        fail() {
          console.log("第一次录音授权失败");
          wx.showModal({
            title: '提示',
            content: '您未授权录音，功能将无法使用',
            showCancel: true,
            confirmText: "授权",
            confirmColor: "#52a2d8",
            success: function (res) {
              if (res.confirm) {
                //确认则打开设置页面（重点）
                wx.openSetting({
                  success: (res) => {
                    console.log(res.authSetting);
                    if (!res.authSetting['scope.record']) {
                      //未设置录音授权
                      console.log("未设置录音授权");
                      wx.showModal({
                        title: '提示',
                        content: '您未授权录音，功能将无法使用',
                        showCancel: false,
                        success: function (res) {

                        },
                      })
                    } else {
                      //第二次才成功授权
                      console.log("设置录音授权成功");
                    }
                  },
                  fail: function () {
                    console.log("授权设置录音失败");
                  }
                })
              } else if (res.cancel) {
                console.log("cancel");
              }
            },
            fail: function () {
              console.log("openfail");
            }
          })
        }
      })

    },
    _handleTouchMove: function (e) {
      var val = {
        e: e
      }
      this.triggerEvent("touchMove",e)
    },
    _handleRecordStop: function (e) {
      var val = {
        e: e
      }
      this.setData({
        luyinzhong: false,
        luyinpop:false
      })
      this.triggerEvent("recordStop",e)
    },

  }
})
